# -*- coding: utf-8 -*-

import json
import re

def convert_to_json_format(file_path, output_path, lines_per_page=50):
    data = []
    page_number = 1
    row_count = 0
    page_content = []

    with open(file_path, "r", encoding="utf-8") as file:
        for line in file:
            line = line.strip()
            if line:
                page_content.append(line)
                row_count += 1

            # 模拟分页：按指定行数分页
            if row_count % lines_per_page == 0:
                page_text = " ".join(page_content)
                data.append({
                    "page": page_number,
                    "allrow": row_count,
                    "type": "text",
                    "inside": page_text
                })
                page_content = []
                page_number += 1

        # 处理最后一页
        if page_content:
            page_text = " ".join(page_content)
            data.append({
                "page": page_number,
                "allrow": row_count,
                "type": "text",
                "inside": page_text
            })

    # 将结果写入JSON文件
    with open(output_path, "w", encoding="utf-8") as output_file:
        json.dump(data, output_file, ensure_ascii=False, indent=4)

def convert_to_json_format_by_page(file_path, output_path):
    data = []
    page_content = []
    page_number = 1
    row_count = 0

    with open(file_path, "r", encoding="utf-8") as file:
        for line in file:
            line = line.strip()
            if line:
                # 检查是否为页码标记
                page_match = re.match(r"^第\s*(\d+)\s*页$", line)
                
                if page_match:
                    # 如果找到新的页码标记，则保存前一页内容
                    if page_content:
                        page_text = " ".join(page_content)
                        data.append({
                            "page": page_number,
                            "allrow": row_count,
                            "type": "text",
                            "inside": page_text
                        })
                        page_content = []
                    
                    # 更新页码
                    page_number = int(page_match.group(1))
                else:
                    # 非页码行则继续累加内容
                    page_content.append(line)
                    row_count += 1

        # 处理最后一页
        if page_content:
            page_text = " ".join(page_content)
            data.append({
                "page": page_number,
                "allrow": row_count,
                "type": "text",
                "inside": page_text
            })

    # 将结果写入 JSON 文件
    with open(output_path, "w", encoding="utf-8") as output_file:
        json.dump(data, output_file, ensure_ascii=False, indent=4)

if __name__ == "__main__":
    # 使用示例
    convert_to_json_format("/home/ubuntu/code/git/subject-word-extraction/scripts/clean_data/000001_2023_平安银行_2023年年度报告_2024-03-15.txt", "/home/ubuntu/code/git/subject-word-extraction/scripts/clean_data/000001_2023_平安银行_2023年年度报告_2024-03-15.json")